#include "stdafx.h"
#include "Camera.h"

CCamera* CCamera::m_pInstance = NULL;

CCamera* CCamera::GetInstance()
{
	if(!m_pInstance)
	{
		m_pInstance = new CCamera();
	}

	return m_pInstance;
}

void CCamera::CameraInit(vec3f Up, vec3f Forward, vec3f Origin)
{
	this->m_vForward = Forward;
	this->m_vUp = Up;
	this->m_vOrigin = Origin;
}

void CCamera::CameraInit()
{
	this->m_vForward.make_zero();
	this->m_vUp.make_zero();
	this->m_vOrigin.make_zero();

	this->m_vForward.z = this->m_vUp.y = 1.0f;

	//let's raise the camera a bit so we can se something :)
	this->m_vOrigin.y = 4.0f;
	this->m_vOrigin.x = -4.0f;
	this->m_vOrigin.z = -4.0f;
}

void CCamera::TranslateCamera(vec3f vTranslate)
{
	//X axis translation, first create an X axis
	if(vTranslate.x != 0)
	{
		vec3f x = cross_product(this->m_vUp, this->m_vForward);
		this->m_vOrigin.x += vTranslate.x*x.x;
		this->m_vOrigin.y += vTranslate.x*x.y;
		this->m_vOrigin.z += vTranslate.x*x.z;
	}

	//Add in Z movement
	this->m_vOrigin.x += vTranslate.z*this->m_vForward.x;
	this->m_vOrigin.y += vTranslate.z*this->m_vForward.y;
	this->m_vOrigin.z += vTranslate.z*this->m_vForward.z;
	
}

void CCamera::RotateCamera(float fDegrees, int nAxis)
{

}

void CCamera::ApplyTransform()
{
	matrix4f temp;
	temp.make_identity();
	temp.axis_pos = this->m_vOrigin;
	temp.axis_z = this->m_vForward;
	temp.axis_y = this->m_vUp;
	temp.axis_x = cross_product(this->m_vUp, this->m_vForward);

	temp.axis_x.scale(-1.0f);
	temp.axis_z.scale(-1.0f);

	matrix4f invCamMat;
	invCamMat = OrthonormalInverse(temp);
	glMultMatrixf(invCamMat.ma);
}

void CCamera::SetAxes(vec3f Yvec, vec3f Zvec)
{
	this->m_vUp = Yvec;
	this->m_vForward = Zvec;
}

matrix4f CCamera::MakeMatrix()
{
	matrix4f m;
	m.make_identity();
	m.axis_pos = this->m_vOrigin;
	m.axis_x = cross_product(this->m_vUp, this->m_vForward);
	m.axis_y = this->m_vUp;
	m.axis_z = this->m_vForward;

	return m;
}